package com.kaver.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

/**
 * prop util
 *
 * @author Roc 2018-02-23 21:57:05
 */
public class PropUtil {
	private static Logger logger = LoggerFactory.getLogger(PropUtil.class);

	public static Properties loadProp(String propertyFileName) {

		if (propertyFileName.startsWith("file:")) {
			propertyFileName = propertyFileName.substring("file:".length());
			return loadFileProp(propertyFileName);
		} else {
			return loadClassPathProp(propertyFileName);
		}
	}

	public static Properties loadClassPathProp(String propertyFileName) {

		InputStream in = null;
		try {
			in = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertyFileName);
			if (in == null) {
				return null;
			}

			Properties prop = new Properties();
			prop.load(new InputStreamReader(in, "utf-8"));
			return prop;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
		return null;
	}

	public static Properties loadFileProp(String propertyFileName) {
		InputStream in = null;
		try {
			File file = new File(propertyFileName);
			if (!file.exists()) {
				return null;
			}

			URL url = new File(propertyFileName).toURI().toURL();
			in = new FileInputStream(url.getPath());
			Properties prop = new Properties();
			prop.load(new InputStreamReader(in, "utf-8"));
			return prop;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
		return null;
	}

	public static boolean writeFileProp(Properties properties, String filePathName) {
		FileOutputStream fileOutputStream = null;
		try {

			File file = new File(filePathName);
			if (!file.exists()) {
				file.getParentFile().mkdirs();
			}
			fileOutputStream = new FileOutputStream(file, false);
			properties.store(new OutputStreamWriter(fileOutputStream, "utf-8"), null);
			return true;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return false;
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
	}
	
	public static Properties loadYmlFileProp(String ymlFileName) {
		try {
			FileSystemResource resource = new FileSystemResource(ymlFileName);
			if (resource.exists()) {
				YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
				yaml.setResources(resource);
				Properties properties = yaml.getObject();
				Properties newproperties = new Properties();
				Set<Object> set = properties.keySet();
				if (set != null && !set.isEmpty()) {
					for (Object key : set) {
						newproperties.put(String.valueOf(key), String.valueOf(properties.get(key)));
					}
				}
				return newproperties;
			}
		}catch(Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}
	public static Properties loadClassPathYmlFileProp(String ymlFileName) {
		try {
			ClassPathResource resource = new ClassPathResource(ymlFileName);
			if (resource.exists()) {
				YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
				yaml.setResources(resource);
				Properties properties = yaml.getObject();
				Properties newproperties = new Properties();
				Set<Object> set = properties.keySet();
				if (set != null && !set.isEmpty()) {
					for (Object key : set) {
						newproperties.put(String.valueOf(key), String.valueOf(properties.get(key)));
					}
				}
				return newproperties;
			}
		}catch(Exception e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}
}
